package com.touch.app.controller;

import com.touch.app.common.domain.Ask;
import com.touch.app.common.util.R;
import com.touch.app.service.RequestServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/api")
public class AskController {

    @Autowired
    private RequestServerService requestServerService;

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @PostMapping("/ask")
    @ResponseBody
    public R ask(@RequestBody Ask ask) {
        String answer = requestServerService.ask(ask);
        if (answer != null && StringUtils.hasLength(answer.trim())) {
            return R.ok(answer);
        }
        return R.error();
    }
}
